package com.longe.generator;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class CodeGenerator {

    static String projectPath = System.getProperty("user.dir");
    static String outputPath = projectPath + "/src/main/java";
    static String xmlOutputPath = projectPath + "/src/main/resources/mapper";

    static String url = "jdbc:mysql://192.168.65.133:3306/adepg?serverTimezone=GMT%2B8";
    static String username = "root";
    static String password = "root";
    static String author = "sky";
    static String parentPackage = "com.longe";
    static String[] tablePrefix = {"t_", "manage_"};


    public static void main(String[] args) {

        Map<String, String> customFile = new HashMap<>();
        customFile.put("DTO.java", "/templates/entityDTO.java.ftl");
        String moduleName = "entity.dto";
        FastAutoGenerator fastAutoGenerator = getFastAutoGenerator(moduleName, customFile);
        fastAutoGenerator.execute();

        customFile.clear();
        customFile.put("VO.java", "/templates/entityVO.java.ftl");
        moduleName = "entity.vo";
        fastAutoGenerator = getFastAutoGenerator(moduleName, customFile);
        fastAutoGenerator.execute();

    }

    private static FastAutoGenerator getFastAutoGenerator(String moduleName, Map<String, String> customFile) {
        return FastAutoGenerator.create(url, username, password)
                    .globalConfig(builder -> {
                        builder.author(author) // 设置作者
                                .enableSwagger() // 开启 swagger 模式
                                //.outputDir("D://"); // 指定输出目录
                                .outputDir(outputPath);
                    })
                    .packageConfig(builder -> {
                        builder.parent(parentPackage) // 设置父包名
                                  .other(moduleName)
                                  //.pathInfo(Collections.singletonMap(OutputFile.xml, "D://111")); // 设置mapperXml生成路径
                                  .pathInfo(Collections.singletonMap(OutputFile.xml, xmlOutputPath)); // 设置mapperXml生成路径
                    })
                    .strategyConfig(builder -> {
                        builder.addTablePrefix(tablePrefix) // 设置过滤表前缀
                               .controllerBuilder()
                                    //分别是开启生成@RestController 控制器
                                    .enableRestStyle()
                                    //开启驼峰转连字符
                                    .enableHyphenStyle()
                                    //覆盖已经生成的文件
                                    .fileOverride()
                               .entityBuilder()
                                    //设置父类
                                    //.superClass("com.longe.entity.BaseEntity")
                                    //添加父类公共字段
                                    //.addSuperEntityColumns("id")
                                    //开启链式模型
                                    .enableChainModel()
                                    //开启 lombok 模型
                                    .enableLombok()
                                    //添加表字段填充
                                    .addTableFills(new Column("create_time", FieldFill.INSERT))
                                    //覆盖已经生成的文件
                                    .fileOverride()
                               .serviceBuilder()
                                    //格式化 service 接口文件名称
                                    .formatServiceFileName("%sService")
                                    //覆盖已经生成的文件
                                    .fileOverride()
                               .mapperBuilder()
                                    //覆盖已经生成的文件
                                    .fileOverride();

                    })
                    .injectionConfig(consumer -> {
                        consumer.customFile(customFile);
                    })

                    .templateEngine(new EnhanceFreemarkerTemplateEngine());
    }

}
